<?php
class String extends Object
{
	public static function Underscore($camel)
	{
	    return implode('_', String::ExplodeCase($camel));
	}

	public static function ExplodeCase($string, $lower = true)
	{
		$array = array();
		$segment = '';
		foreach (str_split($string) as $char)
	    {
		    if (ctype_upper($char))
	        {
				if ($segment)
	                $array[] = $segment;
				$segment = $lower ? strtolower($char) : $char;
			}
	        else
	            $segment .= $char;
		}
		if ($segment)
	        $array[] = $segment;
		return $array;
	}

	public static function Pascalize($underscore_name)
	{
		return str_replace(' ', '', self::Humanize($underscore_name));
	}

    public static function Camelize($underscore_name)
    {
        return self::LowerCaseFirst(self::Pascalize($underscore_name));
    }

	public static function HighlightSql($sql)
	{
		including('geshi/geshi.php');
		$geshi = new GeSHi($sql, 'mysql');
		return $geshi->parse_code();
	}

	public static function RemoveBlankline($string)
	{
		return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
	}

	public static function Humanize($underscore_name)
	{
		return ucwords(str_replace(array('_', '-'), ' ', $underscore_name));
	}

	public static function NullOrEmpty($string)
	{
		return strlen(trim($string)) == 0;
	}

	public static function CropWord($text, $words = 50)
	{
		$matches = preg_split("/\s+/", $text, $words + 1);
		$sz = count($matches);
		if ($sz > $words)
		{
			unset($matches[$sz-1]);
			return implode(' ',$matches)."...";
		}
		return $text;
	}

	public static function Crop($text, $length = 100, $reserve_last_word = false)
	{
        if (strlen($text)>$length)
        {
            if ($reserve_last_word)
            {
                $text = substr($text, 0, $length);
                $text = substr($text, 0, strrpos($text, ' ')).'...';
            }
            else
            {
                $text = substr($text, 0, $length).'...';
            }
        }

        return $text;
	}

	public static function ToHTML($text)
	{
		$text = htmlentities($text);
		$text = preg_replace("/\r\n/", "\n", $text);
		return preg_replace("/\n/", "<br />", $text);
	}

    public static function HumanizeCamel($camel)
	{
		return self::Humanize(self::Underscore($camel));
	}

    public static function BeginWith($begin, $string)
	{
        $begin = self::RegExEscape($begin);
        return preg_match("/^$begin/", $string);
	}

    public static function EndWith($end, $string)
    {
        $end = self::RegExEscape($end);
        return preg_match("/$end$/", $string);
    }

    public static function TrimCardNumber($number)
	{
		return '***'.substr($number, strlen($number)-4);
	}

    public static function ToArray($string)
	{
		$delimiter = ',';
		return explode($delimiter, preg_replace('/'.$delimiter.'\s+/', $delimiter, String::CropEnd(',', trim($string))));
	}

    public static function Contains($needle, $string)
	{
        if (!$needle)
            return true;
		return stripos($string, $needle) !== false;
	}

    public static function ContainsAny($needle, $string)
	{
        if (is_string($needle))
		    $needle = String::ToArray($needle);
        foreach ($needle as $value)
		{
			if (String::Contains($value, $string)) return true;
		}
		return false;
	}

    public static function CropBegin($begin, $string)
	{
        $begin = self::RegExEscape($begin);
		return preg_replace("/^$begin/", null, $string);
	}

    public static function CropEnd($end, $string)
	{
        $end = self::RegExEscape($end);
		return preg_replace("/$end$/", null, $string);
	}

    public static function RegExEscape($string)
    {
        return str_replace('/', '\/', str_replace("\\", "\\\\", $string));
    }

    public static function ToUnixTime($string)
	{
		return strtotime($string);
	}

    public static function ToAscii($string)
    {
        $trans = array(
            'à'=>'a','á'=>'a','ả'=>'a','ã'=>'a','ạ'=>'a',
            'ă'=>'a','ằ'=>'a','ắ'=>'a','ẳ'=>'a','ẵ'=>'a','ặ'=>'a',
            'â'=>'a','ầ'=>'a','ấ'=>'a','ẩ'=>'a','ẫ'=>'a','ậ'=>'a',
            'À'=>'a','Á'=>'a','Ả'=>'a','Ã'=>'a','Ạ'=>'a',
            'Ă'=>'a','Ằ'=>'a','Ắ'=>'a','Ẳ'=>'a','Ẵ'=>'a','Ặ'=>'a',
            'Â'=>'a','Ầ'=>'a','Ấ'=>'a','Ẩ'=>'a','Ẫ'=>'a','Ậ'=>'a',
            'đ'=>'d','Đ'=>'d',
            'è'=>'e','é'=>'e','ẻ'=>'e','ẽ'=>'e','ẹ'=>'e',
            'ê'=>'e','ề'=>'e','ế'=>'e','ể'=>'e','ễ'=>'e','ệ'=>'e',
            'È'=>'e','É'=>'e','Ẻ'=>'e','Ẽ'=>'e','Ẹ'=>'e',
            'Ê'=>'e','Ề'=>'e','Ế'=>'e','Ể'=>'e','Ễ'=>'e','Ệ'=>'e',
            'ì'=>'i','í'=>'i','ỉ'=>'i','ĩ'=>'i','ị'=>'i',
            'Ì'=>'i','Í'=>'i','Ỉ'=>'i','Ĩ'=>'i','Ị'=>'i',
            'ò'=>'o','ó'=>'o','ỏ'=>'o','õ'=>'o','ọ'=>'o',
            'ô'=>'o','ồ'=>'o','ố'=>'o','ổ'=>'o','ỗ'=>'o','ộ'=>'o',
            'ơ'=>'o','ờ'=>'o','ớ'=>'o','ở'=>'o','ỡ'=>'o','ợ'=>'o',
            'Ò'=>'o','Ó'=>'o','Ỏ'=>'o','Õ'=>'o','Ọ'=>'o',
            'Ô'=>'o','Ồ'=>'o','Ố'=>'o','Ổ'=>'o','Ỗ'=>'o','Ộ'=>'o',
            'Ơ'=>'o','Ờ'=>'o','Ớ'=>'o','Ở'=>'o','Ỡ'=>'o','Ợ'=>'o',
            'ù'=>'u','ú'=>'u','ủ'=>'u','ũ'=>'u','ụ'=>'u',
            'ư'=>'u','ừ'=>'u','ứ'=>'u','ử'=>'u','ữ'=>'u','ự'=>'u',
            'Ù'=>'u','Ú'=>'u','Ủ'=>'u','Ũ'=>'u','Ụ'=>'u',
            'Ư'=>'u','Ừ'=>'u','Ứ'=>'u','Ử'=>'u','Ữ'=>'u','Ự'=>'u',
            'ỳ'=>'y','ý'=>'y','ỷ'=>'y','ỹ'=>'y','ỵ'=>'y',
            'Y'=>'y','Ỳ'=>'y','Ý'=>'y','Ỷ'=>'y','Ỹ'=>'y','Ỵ'=>'y'
        );
        return strtr($string, $trans);
    }

    public static function ToUrlFriendly($text)
    {
        $text = self::ToAscii($text);
        $text = preg_replace("/\W/", ' ', $text);
        $text = preg_replace("/\s+/", ' ', $text);
        return strtolower(preg_replace("/\s+/", '-', trim($text)));
    }

    public static function RemoveSpace($text)
    {
        return preg_replace("/\s+/", ' ', trim($text));
    }

    public static function EscapeString($string)
    {
        return addslashes(trim($string));
    }

    public static function Remove($remove, $string)
    {
        return str_replace($remove, null, $string);
    }

    public static function Packjs($js)
    {
        if (AppConfig::GetInstance()->JS_PACK_MODE)
        {
            $packer = new JSpacker($js);
            return $packer->pack();
        }
        else if (AppConfig::GetInstance()->JS_MINIFY_MODE)
            return JSmin::minify($js);
        else
            return $js;
    }

    public static function Packcss($css)
    {
        if (AppConfig::GetInstance()->JS_PACK_MODE || AppConfig::GetInstance()->JS_MINIFY_MODE)
        {
            $min = new CSSmin();
            return $min->minify($css);
        }
        else
            return $css;
    }

    public static function LowerCaseFirst($str)
    {
        return strtolower(substr($str, 0, 1)) . substr($str, 1);
    }

    public static function IsValidEmail($email)
    {
        return preg_match("/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email);
    }

    public static function Plural($num)
    {
        if ($num > 1) return 's';
    }

    public static function RemoveTrailingSlash($url)
    {
        return rtrim($url, FS);
    }
}
?>
